<?php
namespace app\common\model;
use x\Xbase;
    
#服务奖
class Awardfuwu extends Xbase
{  	
	/**
	 * 服务奖
	 * @param  [type] $award_guli_id     [description]
	 * @param  [type] $user_id           [description]
	 * @param  [type] $order_goods_no    [description]
	 * @return [type]                    [description]
	 *
	 * 前提: 必须有人获得鼓励金
		获得鼓励金的人往上的用户获得服务奖
		服务奖在产生鼓励金的基础上才会产生，VIP云店以上等级才会有（不包括VIP云店）
		获得了鼓励金的人不能获得服务奖
		只有零售商及以上才有资格获得
		每个级别只有第一个获得相对应奖金
	 */
	public function doit($award_guli_id,$user_id,$order_goods_no){
		$father = m('User','c')->myfirstfather($user_id);

		$mysql = [];
		foreach ($father as $k=>$v) {
			if ($k<2) continue;

			$money = 0;
			switch ($k) {
				case 2: #零售商
					$money = $order_goods_no['order_total_lingshou'];
					break;
				case 3: #批发商
					$money = $order_goods_no['order_total_pifa'];
					break;
				case 4: #代理商
					$money = $order_goods_no['order_total_daili'];
					break;
			}
			$fee = 0;
        	$real_money = bcsub($money,$fee,2);
        	if ($real_money<=0) continue;

			$data = [
				'order_no' => $this->com_order_no(),
	            'award_guli_id' => $award_guli_id,
	            'order_goods_no_id' => $order_goods_no['id'],
	    		'user_id' => $v['id'],
	    		'level' => $k,
	            'money' => $money,
	            'status' => 1,
	            'note' => '获得服务奖',
	            'fee' => $fee,
	            'real_money' => $real_money,
	    		'create_time' => time(),
			];
			$mysql[] = $order_id = $this->db->insert('award_fuwu',$data);

        	$mysql[] = m('Userbill','xcom')->doit($data['order_no'],$data['user_id'],103,'award_fuwu',$order_id,$data['real_money'],'money_sy','获得服务奖');
		}
		foreach($mysql as $v){if(!$v) return false;}return true;
	}
}
